﻿using Dapper.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Autofac;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ymatou.ServiceCenter.Common;
using Ymatou.ServiceCenter.Domain;
using Ymatou.ServiceCenter.Domain.IRepositories;
using Ymatou.ServiceCenter.Domain.Models;
using Ymatou.ServiceCenter.Services;

namespace Ymatou.ServiceCenter.UnitTest
{
    [TestClass]
    public class RepositoryTest
    {
        #region ServiceMetadata

        [TestMethod]
        public void GetServiceMetadataTest()
        {
            string random = Guid.NewGuid().ToString("N");
            ServiceMetadata serviceMetadata1 = new ServiceMetadata
            {
                AppId = "A" + random,
                ApiName = "M1" + random,
                HttpMethod = "GET",
                ApiPath = "M1",
                Description = "测试",
                CreateTime = DateTime.Now,
                Host = "http://demo.ymatou.com",
                LastUpdateTime = DateTime.Now,
                Owner = "tom"
            };

            bool flag1 = RepositoryHub.ServiceMetadataRepository.Insert(serviceMetadata1);
            Assert.IsTrue(flag1);
            ServiceMetadata serviceMetadata2 =
                RepositoryHub.ServiceMetadataRepository.GetServiceMetadata(serviceMetadata1.AppId,
                    serviceMetadata1.ApiName, serviceMetadata1.HttpMethod);
            Assert.IsNotNull(serviceMetadata2);

            IList<string> appIdList = RepositoryHub.ServiceMetadataRepository.GetAllAppIdList();
            Assert.IsNotNull(appIdList);
            Assert.IsTrue(appIdList.Count > 0);

            bool flag2 = RepositoryHub.ServiceMetadataRepository.Delete(serviceMetadata2);
            Assert.IsTrue(flag2);

        }

        [TestMethod]
        public void GetServiceMetadatasTest()
        {
            IList<ServiceMetadata> serviceMetadatas = new List<ServiceMetadata>();
            string appId = Guid.NewGuid().ToString("N");
            for (int i = 0; i < 5; i++)
            {
                ServiceMetadata serviceMetadata1 = new ServiceMetadata
                {
                    AppId = appId,
                    ApiName = "M" + i,
                    HttpMethod = "GET",
                    ApiPath = "M1",
                    Description = "测试",
                    CreateTime = DateTime.Now,
                    Host = "http://demo.ymatou.com",
                    LastUpdateTime = DateTime.Now,
                    Owner = "tom"
                };
                serviceMetadatas.Add(serviceMetadata1);
            }
            bool flag1 = RepositoryHub.ServiceMetadataRepository.Insert(serviceMetadatas);
            Assert.IsTrue(flag1);

            IList<ServiceMetadata> resultList = RepositoryHub.ServiceMetadataRepository.GetServiceMetadatas(appId, null);
            Assert.IsNotNull(resultList);
            Assert.AreEqual(5, resultList.Count);

            foreach (ServiceMetadata serviceMetadata in serviceMetadatas)
            {
                bool flag2 = RepositoryHub.ServiceMetadataRepository.Delete(serviceMetadata);
                Assert.IsTrue(flag2);
            }
        }

        #endregion

        #region DependencySampling

        [TestMethod]
        public void SaveDependencySamplingTest()
        {
            IList<ServiceDependencySampling> list = new List<ServiceDependencySampling>();
            for (int i = 0; i < 5; i++)
            {
                ServiceDependencySampling sampling = new ServiceDependencySampling
                {
                    ConsumerAppId = "A" + i,
                    ProviderAppId = "B" + i,
                    BeginTime = DateTime.Now.AddDays(-i),
                    EndTime = DateTime.Now.AddDays(i),
                    SamplingElapsedSeconds = 3600,
                    TotalRequestTimes = 100,
                    TotalFailTimes = 2 + i,
                    TotalRequestElapsedMilliseconds = 200 + i,
                };

                list.Add(sampling);
            }
            bool flag1 = RepositoryHub.ServiceDependencySamplingRepository.Insert(list);
            Assert.IsTrue(flag1);

            ServiceDependencySampling sampling1 =
            RepositoryHub.ServiceDependencySamplingRepository.GetServiceDependencySampling("A", "B",
                DateTime.Now.AddDays(-5), DateTime.Now);
            Assert.IsNotNull(sampling1);
        }
        #endregion

        #region ServiceDependency

        [TestMethod]
        public void ServiceDependencyTest()
        {
            IList<ServiceDependencySampling> list1 = new List<ServiceDependencySampling>();
            for (int i = 0; i < 5; i++)
            {
                ServiceDependencySampling sampling = new ServiceDependencySampling
                {
                    ConsumerAppId = "A",
                    ProviderAppId = "B",
                    BeginTime = DateTime.Now.AddDays(-i),
                    EndTime = DateTime.Now.AddDays(i),
                    SamplingElapsedSeconds = 3600,
                    TotalRequestTimes = 100,
                    TotalFailTimes = 2 + i,
                    TotalRequestElapsedMilliseconds = 200 + i,
                };

                list1.Add(sampling);
            }
            bool flag1 = RepositoryHub.ServiceDependencySamplingRepository.Insert(list1);
            Assert.IsTrue(flag1);

            IList<ServiceDependencySampling> list2 = new List<ServiceDependencySampling>();
            for (int i = 0; i < 5; i++)
            {
                ServiceDependencySampling sampling = new ServiceDependencySampling
                {
                    ConsumerAppId = "B",
                    ProviderAppId = "C",
                    BeginTime = DateTime.Now.AddDays(-i),
                    EndTime = DateTime.Now.AddDays(i),
                    SamplingElapsedSeconds = 3600,
                    TotalRequestTimes = 100,
                    TotalFailTimes = 2 + i,
                    TotalRequestElapsedMilliseconds = 200 + i,
                };

                list2.Add(sampling);
            }
            bool flag2 = RepositoryHub.ServiceDependencySamplingRepository.Insert(list2);
            Assert.IsTrue(flag2);

            IList<ServiceDependency> result1 = RepositoryHub.ServiceDependencyRepository.MyConsumerList("B");
            Assert.IsNotNull(result1);

            IList<ServiceDependency> result2 = RepositoryHub.ServiceDependencyRepository.MyProviderList("A");
            Assert.IsNotNull(result2);
        }

        #endregion

        #region ServiceConfig

        [TestMethod]
        public void TestServiceConfig()
        {
            IList<ServiceConfig> serviceConfigs = new List<ServiceConfig>();
            int total = 5;
            for (int i = 0; i < total; i++)
            {
                ServiceConfig serviceConfig = new ServiceConfig
                {
                    ConsumerAppId = Guid.NewGuid().ToString("N"),
                    ProviderAppId = Guid.NewGuid().ToString("N"),
                    IsDisabled = true
                };
                serviceConfigs.Add(serviceConfig);
            }
            bool flag1 = RepositoryHub.ServiceConfigRepository.Insert(serviceConfigs);
            Assert.IsTrue(flag1);

            foreach (ServiceConfig serviceConfig in serviceConfigs)
            {
                ServiceConfig config =
                    RepositoryHub.ServiceConfigRepository.GetServiceConfig(serviceConfig.ConsumerAppId,
                        serviceConfig.ProviderAppId);

                Assert.IsNotNull(config);

                bool flag = RepositoryHub.ServiceConfigRepository.Delete(serviceConfig);
                Assert.IsTrue(flag);
            }
        }

        [TestMethod]
        public void TestServiceConfig2()
        {
            ServiceConfig serviceConfig = new ServiceConfig
            {
                ConsumerAppId = "A",
                ProviderAppId = "B-" +Guid.NewGuid().ToString("N"),
                IsDisabled = true
            };
            bool flag= RepositoryHub.ServiceConfigRepository.Insert(serviceConfig);
            Assert.IsTrue(flag);
        }

        [TestMethod]
        public void TestServiceConfig3()
        {
            ServiceConfig serviceConfig = RepositoryHub.ServiceConfigRepository.GetServiceConfig("A", "B");
            if (serviceConfig != null)
            {
                serviceConfig.IsDisabled = false;
                serviceConfig.LastUpdateTime=DateTime.Now;
               bool flag = RepositoryHub.ServiceConfigRepository.Update(serviceConfig);
               Assert.IsTrue(flag);
            }
        }

        [TestMethod]
        public void TestGetServicedependencyInfo()
        {
            var ConsumerAppId = "userservice.ymatou.com211";
            var ProviderAppId = "auth.ymatou.com";
            var dependency = RepositoryHub.ServicedependencyInfoRepository.GetById(new { ConsumerAppId = ConsumerAppId, ProviderAppId = ProviderAppId });

            Assert.IsNull(dependency);
        }

        [TestMethod]
        public void TestUpdateServicedependencyInfo()
        {
            var ConsumerAppId = "userservice.ymatou.com";
            var ProviderAppId = "auth.ymatou.com";

            var dependency = RepositoryHub.ServicedependencyInfoRepository.GetById(new { ConsumerAppId = ConsumerAppId, ProviderAppId = ProviderAppId });
            dependency.LastUpdateTime = DateTime.Now;

            bool updateResult = RepositoryHub.ServicedependencyInfoRepository.Update(dependency);

            Assert.IsTrue(updateResult);
        }

        #endregion
    }
}
